﻿$(function(){
    var request = moon.getRequest(location.search); //获取url中"?"符后的参数
    var sensorId = request['sensorId'];  //获取传感器sensor_id
    var deviceId ;
    //填充数据
    $.getJsonData(contextPath+"/sensor/"+sensorId,{},{type:"Get"}).done(function (data) {
        console.log(data);
        if(data.success){
            var result = data.result;
            deviceId = result.deviceId;
            console.log(result.period);
            $("#title").val(result.title);
            $("#tags").val(result.tags);
            $("#unit").val(result.unit);
            $("#formula").val(result.formula);
            $("#period").val(result.period);
            $("#time").val(result.time);
            $("#about").val(result.about);
        }
    })

    $("#loginOut").on("click",function(){
        $.getJsonData(fullServerPath+"/users/logout",{},{type:"Post"}).done(function(data){
            if(data.success){
                moon.info("退出","top");
                window.location.href = contextPath+"login.html";
            }else{
                moon.error("退出失败","top");
            }
        })
    })
    //更新
    $("#submit").click(function(){
        $("#sensorForm").validate("validate").done(function(result){
            var sensor={
                type: $("#selectType").val(),
                title:$("#title").val(),
                tags:$("#tags").val(),
                unit:$("#unit").val(),
                formula:$("#formula").val(),
                period:$("#period").find("option:selected").text(),
                time:$("#time").val(),
                about:$("#about").val()
            }
            console.log(sensor);
            $.getJsonData(contextPath+"/sensor/"+sensorId,sensor,{type:"Put"}).done(function(data){
                console.log(data);
                if(data.success){
                    //moon.info("update success","top");
                    //成功跳转
                    window.location.href=contextPath+"sen_suc_edit.html?deviceId="+deviceId;
                }else{
                    moon.error("fail","top");
                }
            }).fail(function(e){
                console.log(e);
            });
        })
    })

    //添加表单验证
    $("#sensorForm").validate({align:'bottom',theme:"darkblue"});

    $(document).ready(function(){
        var data = $("#selectType  option:selected").val();
        if(data == 2 || data==3){
            $("#wendu").attr("disabled",true);
            $("#wendu").attr("placeholder","此类型不需要输入");
            $("#dataChange").attr("disabled",true);
            $("#dataChange").attr("placeholder","此类型不需要输入");
            $("#time").attr("disabled",true);
        }
        if(data == 1){
            $("#wendu").attr("disabled",false);
            $("#wendu").attr("placeholder","摄氏度/");
            $("#dataChange").attr("disabled",false);
            $("#dataChange").attr("placeholder","支持带有括号的四则运算, v代表当前值，示例：(v + 1) * 1 + 10");
            $("#time").attr("disabled",false);
        }
    });
})